package com.xgimi.open.skill.datamanager.biz.common.aop;

import com.xgimi.commons.base.Result;
import com.xgimi.commons.exception.CommonCode;
import com.xgimi.open.skill.datamanager.biz.common.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * @author coderzpw.zhang
 * @version 1.0
 * @description 处理的RPC服务异常
 * @date 2023/3/5 23:04
 */
@Slf4j
@Component
@Aspect // 声明该类为一个切面类
public class RpcExceptionHandle implements Serializable {
    private static final long serialVersionUID = 7592063743537066800L;


    @Pointcut(value = "execution(* com.xgimi.open.skill.datamanager.service.impl..*.*(..))")
    public void pointCut() {
    }

    @Around("pointCut()")
    public Object handle(ProceedingJoinPoint joinPoint) {
        try {
            Object proceed = joinPoint.proceed();
            return proceed;
        } catch (BizException bizException) {
            log.info("BizException ==>", bizException);
            return new Result(bizException.getCode(), bizException.getMessage());
        } catch (RuntimeException runtimeException) {
            log.info("runtimeException ==>", runtimeException);
            return new Result(CommonCode.SYS_ERROR.getCode(), runtimeException.getMessage());
        } catch (Throwable throwable) {
            log.info("throwable ==>", throwable);
            return new Result(CommonCode.SYS_ERROR.getCode(), throwable.getMessage());
        }
    }
}
